home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Magazine / C_Tutorial / Part-2 / mouse2.c < prev    next >
C/C++ Source or Header  |  1997-07-01  |  2KB  |  101 lines

  1. #include<exec/libraries.h>
  2. #include<intuition/intuition.h>
  3. #include<utility/tagitem.h>
  4. #include<graphics/text.h>
  5. #include<graphics/rastport.h>
  6.  
  7. #include<string.h>
  8.  
  9. #include<clib/exec_protos.h>
  10. #include<clib/graphics_protos.h>
  11. #include<clib/intuition_protos.h>
  12.  
  13. struct Library* GfxBase;
  14. struct Library* IntuitionBase;
  15.  
  16. /* Need to give a prototype for our message handling function */
  17. void handleIDCMP(struct Window*);
  18.  
  19. void main()
  20. {
  21.     /* Open libraries... */
  22.     if(GfxBase = OpenLibrary("graphics.library",36))
  23.     {
  24.         if(IntuitionBase = OpenLibrary("intuition.library",36))
  25.         {
  26.             /* Open our window */
  27.             struct Window* win;
  28.             if(win = OpenWindowTags(NULL,
  29.                                                             WA_Left,        20,
  30.                                                             WA_Top,            20,
  31.                                                             WA_Width,        200,
  32.                                                             WA_Height,    100,
  33.                                                             WA_Flags,        WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_REPORTMOUSE,
  34.                                                             WA_IDCMP,        IDCMP_CLOSEWINDOW | IDCMP_MOUSEBUTTONS | IDCMP_MOUSEMOVE,
  35.                                                             TAG_DONE,        0))
  36.             {
  37.                 /* If window opened, handle its IDCMP messages */
  38.                 handleIDCMP(win);
  39.                 CloseWindow(win);
  40.             }
  41.             CloseLibrary(IntuitionBase);
  42.         }
  43.         CloseLibrary(GfxBase);
  44.     }
  45. }
  46.  
  47. /* Our message handling code */
  48. void handleIDCMP(struct Window* win)
  49. {
  50.     char* text = "Hello World!";
  51.     int going = TRUE;
  52.     int drawing = FALSE;
  53.     UBYTE pen = 1;
  54.     SetDrMd(win->RPort, JAM1);
  55.     /* Loop, waiting for messages, until the close gadget clicked */
  56.     while(going)
  57.     {
  58.         struct IntuiMessage* intuimsg;
  59.         /* Wait for messages to arrive */
  60.         WaitPort(win->UserPort);
  61.         /* Messages have arrived: loop through all of them */
  62.         while(intuimsg = (struct IntuiMessage*)GetMsg(win->UserPort))
  63.         {
  64.             /* Act on this message... */
  65.             switch(intuimsg->Class)
  66.             {
  67.             case IDCMP_MOUSEBUTTONS:
  68.                 switch(intuimsg->Code)
  69.                 {
  70.                 case SELECTDOWN:
  71.                     drawing = TRUE;
  72.                     break;
  73.                 case SELECTUP:
  74.                     drawing = FALSE;
  75.                     break;
  76.                 }
  77.                 /* Omit the break to draw on click, too */
  78.                 break;
  79.             case IDCMP_MOUSEMOVE:
  80.                 if(drawing)
  81.                 {
  82.                     /* Try changing colour using something like this: */
  83.                     SetAPen(win->RPort, pen++);
  84.                     /* Or this: */
  85.                     /* SetAPen(win->RPort, (UBYTE)(intuimsg->MouseY / 8)); */
  86.                     /* Or something of your own invention... */
  87.                     Move(win->RPort, intuimsg->MouseX, intuimsg->MouseY);
  88.                     Text(win->RPort, text, strlen(text));
  89.                     RefreshWindowFrame(win);
  90.                 }
  91.                 break;
  92.             case IDCMP_CLOSEWINDOW:
  93.                 going = FALSE;
  94.                 break;
  95.             }
  96.             /* Reply when finished with message */
  97.             ReplyMsg((struct Message*)intuimsg);
  98.         }
  99.   }
  100. }
  101.